package com.elm.controller;

import java.io.IOException;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.elm.model.Discuss;
import com.elm.model.Food;
import com.elm.model.PageBean;
import com.elm.model.Seller;
import com.elm.model.User;
import com.elm.service.DiscussService;
import com.elm.service.FavoriteService;
import com.elm.service.FoodService;
import com.elm.service.SellerService;
import com.elm.serviceimpl.DiscussServiceImpl;
import com.elm.serviceimpl.FavoriteServiceImpl;
import com.elm.serviceimpl.FoodServiceImpl;
import com.elm.serviceimpl.SellerServiceImpl;
import com.elm.util.PageUtil;

public class ShowFoodController extends HttpServlet{
	SellerService ss = new SellerServiceImpl();
	FoodService fs = new FoodServiceImpl();
	DiscussService ds = new DiscussServiceImpl();
	FavoriteService favoriteservice = new FavoriteServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf8");
		HttpSession session = req.getSession();
		String fp =  req.getParameter("foodpage");
		int userid = ((User)session.getAttribute("currentUser")).getUserid();
		int sellerid =Integer.parseInt(req.getParameter("id"));
		 int  foodpage = 0;
		  if(fp==null){
			  foodpage=1;
		  }else{
			   foodpage = Integer.parseInt(fp);
		  }
		  PageBean   foodpagebean = new PageBean(foodpage, 8);
		  session.setAttribute("sellerid", sellerid);
		Seller s = ss.findSellerById(sellerid);
		session.setAttribute("aseller", s);
		List<Food> foods = fs.findAllfoodBySid(sellerid,foodpagebean);
		session.setAttribute("foodpage", this.getFoodPage(fs.getFoodCount(sellerid), foodpage, foodpagebean.getPageSize(),sellerid));
		session.setAttribute("foods", foods);
		String dp =  req.getParameter("discusspage");
		 int  discusspage = 0;
		  if(dp==null){
			  discusspage=1;
		  }else{
			  discusspage = Integer.parseInt(dp);
		  }
		  PageBean   discusspagebean = new PageBean(discusspage, 6);
		List<Discuss> discuss =ds.getAllDiscussBySid(sellerid, discusspagebean);
		session.setAttribute("discusspage", this.getDiscussPage(ds.getDiscussCount(sellerid), discusspage, discusspagebean.getPageSize(),sellerid));
		session.setAttribute("discuss", discuss);
		if(favoriteservice.isFavoriteBySidAndUid(userid, sellerid)){
			req.setAttribute("favorite", "<a href='changefavorite?id="+sellerid+"&flag=1'><img alt='' src='/hungry_ELM/img/save.png'></a>");
		}
		else{
			req.setAttribute("favorite", "<a href='changefavorite?id="+sellerid+"&flag=0'><img alt='' src='/hungry_ELM/img/save1.png'></a>");
		}
		req.getRequestDispatcher("seller/showfood.jsp").forward(req, resp);
	}
	private  String  getFoodPage(int totalNum,int currentPage,int pageSize,int sellerid){
		int totalPage = totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;
		StringBuffer page =  new StringBuffer();
		page.append("<li><a href='showfood?id="+sellerid+"&foodpage=1'>首页</a></li>");
		if(currentPage==1){
			page.append("<li class='disabled'><a href=''>上一页</a></li>");
		}else{
			page.append("<li><a href='showfood?id="+sellerid+"&foodpage="+(currentPage-1)+"'>上一页</a></li>");
		}
		//生成页码     当前页 2        0                  4
		for (int i = currentPage-2; i < currentPage+2; i++) {
				if(i<1||i>totalPage){
					continue;
				}
				if(i==currentPage){
					page.append("<li class='active'><a href='#'>"+i+"</a></li>");
				}else{
					page.append("<li><a href='showfood?id="+sellerid+"&foodpage="+i+"'>"+i+"</a></li>");
				}
		}
		if(currentPage==totalPage){
			page.append("<li class='disabled'><a href=''>下一页</a></li>");
		}else{
			page.append("<li><a href='showfood?id="+sellerid+"&foodpage="+(currentPage+1)+"'>下一页</a></li>");
		}
	
		
		page.append("<li><a href='showfood?foodpage="+totalPage+"'>尾页</a></li>");
		return  page.toString();
	}
	private  String  getDiscussPage(int totalNum,int currentPage,int pageSize,int sellerid){
		int totalPage = totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;
		StringBuffer page =  new StringBuffer();
		page.append("<li><a href='showfood?id="+sellerid+"&discusspage=1'>首页</a></li>");
		if(currentPage==1){
			page.append("<li class='disabled'><a href=''>上一页</a></li>");
		}else{
			page.append("<li><a href='showfood?id="+sellerid+"&discusspage="+(currentPage-1)+"'>上一页</a></li>");
		}
		//生成页码     当前页 2        0                  4
		for (int i = currentPage-2; i < currentPage+2; i++) {
				if(i<1||i>totalPage){
					continue;
				}
				if(i==currentPage){
					page.append("<li class='active'><a href='#'>"+i+"</a></li>");
				}else{
					page.append("<li><a href='showfood?id="+sellerid+"&discusspage="+i+"'>"+i+"</a></li>");
				}
		}
		if(currentPage==totalPage){
			page.append("<li class='disabled'><a href=''>下一页</a></li>");
		}else{
			page.append("<li><a href='showfood?id="+sellerid+"&discusspage="+(currentPage+1)+"'>下一页</a></li>");
		}
	
		
		page.append("<li><a href='showfood?discusspage="+totalPage+"'>尾页</a></li>");
		return  page.toString();
	}
}
